package uumoi

import grails.plugin.springsecurity.annotation.Secured
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContextHolder

@Secured(['ROLE_USER'])
class AuthController {
	def springSecurityService
	def logoutHandlers


	def init() {
		User user = User.get(springSecurityService.currentUser.id)
		session.user = user.id

		Set<Account> accounts = user.accounts

		if (accounts.size() > 1)
			render model: ['accounts': accounts], view:'selectAccount'
		else {

			session['account'] = accounts[0].id
			redirect controller: 'collection', action: 'index'
		}
	}


	def logout () {
		Authentication auth = SecurityContextHolder.context.authentication
		if (auth) {
			logoutHandlers.each  { handler->
				handler.logout(request,response,auth)
			}
		}
		redirect uri: params.redirect
	}
}
